package com.thb.base.dao.Impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.thb.base.dao.IVersionDao;
import com.thb.base.model.Version;

@Repository
public class VersionDaoImpl implements IVersionDao {
	
	@Autowired
	private JdbcTemplate jTemplate;
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public  List<Version> findAll() {
		String sql = "select version_number,inner_version,description,file_url from version order by inner_version";
		Object[] arg = new Object[] { };
		
		return jTemplate.query(sql, arg,new RowMapper() {
			public Object mapRow(ResultSet rs, int no) throws SQLException {

				return jsonMapper(rs, no);
			}
		});
	}

	private Version jsonMapper(ResultSet rs, int no) throws SQLException {

		Version vo = new Version();
		vo.setVersionNumber(rs.getString("version_number"));
		vo.setInnerVersion(rs.getInt("inner_version"));
		vo.setDescription(rs.getString("description"));
		vo.setFileUrl(rs.getString("file_url"));
	
		return vo;
	}

}
